﻿
package com.ostrich.project.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.service.BaseService;
import com.base.util.DateUtil;
import com.base.util.SessionUtils;
import com.ostrich.manpower.common.FormulaUtil;
import com.ostrich.project.bean.TostPjCostitem;
import com.ostrich.project.service.TostPjCostitemDao;
/**
 * @author wangyinan
 * @brief    模块功能简单描述
 * @details  模块功能详细描述
 */
@Service("tostPjCostitemService")
public class TostPjCostitemService<T> extends BaseService<T> {
	private final static Logger log= Logger.getLogger(TostPjCostitemService.class);

	@Autowired
    private TostPjCostitemDao<T> dao;
	
	public TostPjCostitemDao<T> getDao() {
		return dao;
	}
	
	@Autowired
	private TostPjPushmoneyService<T> tostpjpushmoneyservice; 
	@Autowired
	private TostPjOfficedetailService<T> tostpjofficedetailservice;
	@Autowired
	private TostPjGeneraltableService<T> tostpjgeneraltableservice;
	@Autowired
	private TostPjEngdepartcostService<T> tostpjengdepartcostservice;

	//查询费用表ID、费用ID和名称
	public List<TostPjCostitem> queryCostitem_to_others()
	{
		List<TostPjCostitem> list=new ArrayList<TostPjCostitem>();
		list=dao.queryCostitem_to_others();
		return list;
	}
	
	public Integer addCost(TostPjCostitem tpc){
		return dao.addCost(tpc);
	}
	public void deleteByid(TostPjCostitem pjcost)throws Exception{
		//为空
		Integer id = pjcost.getId();
		dao.deleteByid(id);
		
		if(pjcost.getPctType()==61){
			tostpjpushmoneyservice.deleteByPctId(id);
		}
		else if(pjcost.getPctType()==62){
			tostpjofficedetailservice.deleteByPctId(id);
		}
		else if(pjcost.getPctType()==63){
			tostpjgeneraltableservice.deleteByPctId(id);
		}
		else if(pjcost.getPctType()==64){
			tostpjengdepartcostservice.deleteByPctId(id);
		}
		
		if (pjcost != null && pjcost.getPctName() != null
				&& pjcost.getPctName().trim().length() > 0) {                         
			                                                                             
				//算法                                              
				Map<String,Object> fMap = FormulaUtil.formulaById(11, pjcost.getPctTotal());  
				if(fMap!=null){                                                            
					pjcost.setPctPraise((Float)fMap.get("base"));                   
					pjcost.setPctPraise((Float)fMap.get("price"));                   
					pjcost.setPctTotal((Float)fMap.get("amount"));                                
				}                                                     
				dao.add((T) pjcost);  
		}
	}
	public List<TostPjCostitem> queryBypjId(int pjId){
		return dao.queryBypjId(pjId);
	}
	
	
	public Map<String,Object> getPjTotalByPjId(Integer pjId){
		Map<String,Object> mp = FormulaUtil.formulaById(9, pjId);
		boolean isExist= false;
		Map<String,Object> result = new HashMap<String,Object>();
		Map<Integer,Object> totalmap =new HashMap<Integer,Object>();
		Map<Integer,Object> praisemap = new HashMap<Integer,Object>();
		if(mp==null)
			isExist=false;
		else{
			List<Map<String,Object>> ls = (List<Map<String, Object>>) mp.get("data");
			for(Map<String,Object> cost:ls){
				Long ct = (Long) cost.get("cout");
				Long type =(Long)cost.get("PCT_TYPE");
				Double total =(Double )cost.get("PCT_BUDGET");
				Double praise =(Double )cost.get("PCT_PRAISE");
				//61、项目提成；62、非现金支出（提成职员）；63、办公费用；
				//64、工程技术部人工费用；65、其他(工会经费/其他)；66、公共支撑部门费用分摊；67、税金；
				if(type==61||type==62||type==64||type==65||type==66){
					if(ct!=null && ct>0)
						isExist=true;
					else
						isExist=false;
				}
				totalmap.put(type.intValue(), total);
				praisemap.put(type.intValue(), praise);
			}
		}
		
		result.put("isExist", isExist);
		result.put("totalmap", totalmap);
		result.put("praisemap", praisemap);
		return result;
	}
	
	public void addCostitem(TostPjCostitem pjcost,HttpServletRequest request,int type) throws Exception{
		pjcost.setPctType(type);
		List<TostPjCostitem> dataList = (List<TostPjCostitem>) this.queryByList(pjcost);
		
		Map<String,Object> result = getPjTotalByPjId(pjcost.getPjId());
		Map<Integer,Object> totalmap =(result!=null && result.get("totalmap")!=null)?(Map<Integer,Object>) result.get("totalmap"):new HashMap<Integer,Object>();
		Map<Integer,Object> praisemap =(result!=null && result.get("praisemap")!=null)?(Map<Integer,Object>) result.get("praisemap"):new HashMap<Integer,Object>();
		//61、项目提成；62、非现金支出（提成职员）；63、办公费用；
		//64、工程技术部人工费用；65、其他(工会经费/其他)；66、公共支撑部门费用分摊；67、税金；
		Float pjpushmoney =(totalmap.get(61)!=null)?Float.valueOf(((Double)totalmap.get(61)).intValue()):new Float(0);
		Float pjnocash =(totalmap.get(62)!=null)?Float.valueOf(((Double)totalmap.get(62)).intValue()):new Float(0);
		Float pjengineercose  =(totalmap.get(64)!=null)?Float.valueOf(((Double)totalmap.get(64)).intValue()):new Float(0);
		Float pjother  = (totalmap.get(65)!=null)?Float.valueOf(((Double)totalmap.get(65)).intValue()):new Float(0);
		Float pjpubsupport  = (totalmap.get(66)!=null)?Float.valueOf(((Double)totalmap.get(66)).intValue()):new Float(0);
		
		Float pjpushpraise = (praisemap.get(61)!=null)?Float.valueOf(((Double)praisemap.get(61)).intValue()):new Float(0);
		Float pjnocashpraise =(praisemap.get(62)!=null)?Float.valueOf(((Double)praisemap.get(62)).intValue()):new Float(0);
		Float pjengineerpraise = (praisemap.get(64)!=null)?Float.valueOf(((Double)praisemap.get(64)).intValue()):new Float(0);
		Float pjotherpraise  = (praisemap.get(65)!=null)?Float.valueOf(((Double)praisemap.get(65)).intValue()):new Float(0);
		Float pjpubsupportpraise  = (praisemap.get(66)!=null)?Float.valueOf(((Double)praisemap.get(66)).intValue()):new Float(0);
		
		//公共支撑费用分摊
		Map bdgmap = FormulaUtil.formulaById(26, pjpushmoney,pjnocash,pjengineercose,pjpushpraise,pjengineerpraise);
		Float bdg=(bdgmap!=null)?(Float)bdgmap.get("publicsupport"):new Float(0);
		Float bdgpraise=(bdgmap!=null)?(Float)bdgmap.get("publicpraise"):new Float(0);
		Float bdgtotal = bdg+bdgpraise;
		
		//税金
		Map taxesmap = FormulaUtil.formulaById(27, pjpushmoney,pjnocash,pjengineercose,pjother,pjpubsupport,pjpushpraise,pjnocashpraise,pjengineerpraise,pjotherpraise,pjpubsupportpraise);
		Float taxes=(taxesmap!=null)?(Float)taxesmap.get("taxessupport"):new Float(0);
		Float taxespraise=(taxesmap!=null)?(Float)taxesmap.get("taxespraise"):new Float(0);
		Float taxestotal = taxes+taxespraise;
		
		//其他（公会经费）
		Map othermap = FormulaUtil.formulaById(28,pjpushmoney,pjengineercose,pjpushpraise,pjengineerpraise);
		Float other=(taxesmap!=null)?(Float)othermap.get("othersupport"):new Float(0);
		Float otherpraise=(taxesmap!=null)?(Float)othermap.get("otherpraise"):new Float(0);
		Float othertotal = other+otherpraise;
		
		if(dataList==null || dataList.size()<=0){
			//判断类型为公共费用分摊  还是 税金
			if(type==66){
				//设置公共支撑费用合计及奖励费用，合计
				pjcost.setPctBudget(bdg);
				pjcost.setPctPraise(bdgpraise);
				pjcost.setPctTotal(bdgtotal);
			}else if(type==67){
				//设置税金
				pjcost.setPctBudget(taxes);
				pjcost.setPctPraise(taxespraise);
				pjcost.setPctTotal(taxestotal);
			}else if(type==65){
				//设置 其他（公会经费）
				pjcost.setPctBudget(other);
				pjcost.setPctPraise(otherpraise);
				pjcost.setPctTotal(othertotal);
			}
			
			pjcost.setCreatetime(DateUtil.getNowPlusTime());
			pjcost.setPerson(SessionUtils.getUser(request).getEmail());
			this.add((T) pjcost);
		}else{
			TostPjCostitem tp = dataList.get(0);
			//判断类型为公共费用分摊  还是 税金
			if(type==66){
				//设置公共支撑费用合计及奖励费用，合计
				pjcost.setPctBudget(bdg);
				pjcost.setPctPraise(bdgpraise);
				pjcost.setPctTotal(bdgtotal);
			}else if(type==67){
				//设置税金 
				pjcost.setPctBudget(taxes);
				pjcost.setPctPraise(taxespraise);
				pjcost.setPctTotal(taxestotal);
			}else if(type==65){
				//设置 其他（公会经费）
				pjcost.setPctBudget(other);
				pjcost.setPctPraise(otherpraise);
				pjcost.setPctTotal(othertotal);
			}
			
			pjcost.setId(tp.getId());
			this.update((T) pjcost);
		}
	}
}
